function getTop(obj) { // 返回一个元素距离页面顶端的距离
    var iTop = 0;
    while (obj) {
        iTop += obj.offsetTop; // 累加
        obj = obj.offsetParent; // obj 的父元素
    }
    return iTop;
}

function loadImage(Img) { // 图片按需加载
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    var isLoad = false;
    for (var i = 0; i < Img.length; i++) {
        if (!Img[i].isLoad && (getTop(Img[i]) < (scrollTop + document.documentElement.clientHeight))) {
            Img[i].isLoad = true;
            Img[i].src = Img[i].getAttribute('_src') || Img[i].src;
        }
    }
}

function imgload() { // 图片延迟加载 这是在， webapp下面运行的函数， 所以不用 写 onresize
    var Aimg = document.getElementsByTagName('img');
    loadImage(Aimg);
    window.addEventListener('scroll', function() {
        loadImage(Aimg);
    }, false);
    window.addEventListener('resize', function() {
        loadImage(Aimg);
    }, false);
}

function TabQieHuan(nav) { // tab切换
    if (! nav)
        return;
    var aLi = nav.getElementsByTagName('li'),
        aIndex_tab = document.getElementsByClassName('tab-qiehuan');
    for (var i = 0; i < aLi.length; i++) {
        aLi[i].index = i;
        aLi[i].onclick = function() {
            for (var i = 0; i < aLi.length; i++) {
                aLi[i].className = '';
                aIndex_tab[i].style.display = 'none';
            }
            this.className = 'active';
            aIndex_tab[this.index].style.display = 'block';
        };
    }
}

// function autoHeight(box) { // 轮播图高度js 控制
//     var aIndex_banner_img = box.getElementsByTagName('img');
//     oWidth = box.offsetWidth;
//     box.style.height = oWidth / (1039 / 472) + 'px';
//     for (var i = 0; i < aIndex_banner_img.length; i++) {
//         aIndex_banner_img[i].style.height = box.style.height;
//     }
// }

function banner(ele, shijian) {
    var oUl = ele.getElementsByTagName('ul')[0],
        aLiUl = oUl.getElementsByTagName('li'),
        oOl = ele.getElementsByTagName('ol')[0],
        aLiOl = oOl.getElementsByTagName('li'),
        animated = true, // 定义表示判断是否滑动完成
        n = 1,
        aIconLen = aLiOl.length,
        width = aLiUl[0].offsetWidth,
        autoTimer = null;
    var li1 = document.createElement('li');
    li1.innerHTML = aLiUl[0].innerHTML;
    oUl.appendChild(li1);
    var li2 = document.createElement('li');
    li2.innerHTML = aLiUl[aIconLen - 1].innerHTML;
    oUl.appendChild(li2);
    oUl.insertBefore(li2, aLiUl[0]);
    var aImgLen = aLiUl.length;
    oUl.style.width = width * aImgLen + 'px'; // 求出ul的寬度
    oUl.style.position = 'absolute';
    for (var i = 0; i < aImgLen; i++) {
        aLiUl[i].style.width = oUl.offsetWidth / aImgLen + 'px'; // 因为每个li设置了 winth 为 100%， 如果不除以6 则 每个li等于ul的宽度
    }
    oUl.style.left = -width + 'px';

    function showButton() { // 小按钮函数
        for (var j = 0; j < aIconLen; j++) {
            aLiOl[j].className = '';
        }
        aLiOl[n - 1].className = 'active';
    }

    function animate(offset) { // 运动函数
        animated = false;
        var newLeft = oUl.offsetLeft + offset; // 得出目标值
        // var speed = offset / 10;  匀速运动
        function go() {
            var speed = (newLeft - oUl.offsetLeft) / 10; // 缓冲运动
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); // 速度一定要取整
            if (newLeft === oUl.offsetLeft) {
                animated = true;
                if (newLeft < -width * aIconLen) {
                    oUl.style.left = -width + 'px';
                }
                if (newLeft > -width) {
                    oUl.style.left = -width * aIconLen + 'px';
                }
            } else {
                oUl.style.left = oUl.offsetLeft + speed + 'px';
                setTimeout(go, 10);
            }
        }
        go();
    }
    for (var j = 0; j < aIconLen; j++) {
        aLiOl[j].index = (j + 1);
        aLiOl[j].onclick = function() {
            if (!animated) {
                return;
            }
            if (this.className === 'active') {
                return;
            }
            var myIndex = this.index;
            var offset = -width * (myIndex - n);
            n = myIndex;
            animate(offset);
            showButton();
        };
    }
    var hammertime = new Hammer(ele); // 新建 hammertime 对象
    hammertime.on('swipeleft', function(e) {
        if (!animated) {
            return;
        }
        if (n === aIconLen) {
            n = 1;
        } else {
            n += 1;
        }
        animate(-width);
        showButton();
    });
    hammertime.on('swiperight', function(e) {
        if (!animated) {
            return;
        }
        if (n === 1) {
            n = aIconLen;
        } else {
            n -= 1;
        }
        animate(width);
        showButton();
    });

    function play() {
        autoTimer = setInterval(function() {
            if (!animated) {
                return;
            }
            if (n === aIconLen) {
                n = 1;
            } else {
                n += 1;
            }
            animate(-width);
            showButton();
        }, shijian);
    }
    play();
}

function fixNav(obj) { // nav 固定效果
    var oTop = getTop(obj);
    var aTabqiehuan = document.getElementsByClassName('tab-qiehuan');

    function fix() {
        var scrollTop = document.body.scrollTop;
        if (scrollTop > oTop) {
            obj.className = 'nav fixNav';

        }
        if (scrollTop < oTop) {
            obj.className = 'nav';
        }
    }
    window.addEventListener('scroll', function() {
        fix();
    }, false);
    window.addEventListener('resize', function() {
        fix();
    }, false);
}

function like(obj) {
    obj.addEventListener('click', function(e) {
        if (e.target.tagName.toUpperCase() === 'DIV') {
            if (e.target.className === 'like black') {
                e.target.className = 'like red';
                e.target.innerHTML++;
            } else {
                e.target.className = 'like black';
                e.target.innerHTML--;
            }
        }
    });
}

function imgList(obj) {
    var aImg = obj.getElementsByTagName('img');
    var aLi = obj.getElementsByTagName('li');
    var aLibg = obj.getElementsByClassName('libg');

    if (aLibg.length > 0)
    {
        oWidth = aImg[0].offsetWidth;
        for (var i = 0; i < aImg.length; i++) {
            aLibg[i].style.height = aLi[i].style.height = aImg[i].style.height = oWidth / (689 / 297) + 'px';
        }
    }
}

function autoCenter(obj) {
    juzhong(obj);
    window.addEventListener('scroll', function() {
        juzhong(obj);
    }, false);
    window.addEventListener('resize', function() {
        juzhong(obj);
    }, false);
}

function juzhong(obj) { // 喜欢的礼物， 喜欢的攻略 垂直水平居中
    var bodyHeight = document.documentElement.clientHeight; // 窗口高度
    var baseHeight = bodyHeight - 42; // 固定高度
    var aBase = document.getElementsByClassName('percen-base');
    aBase[0].style.height = baseHeight + 'px';
    aBase[1].style.height = baseHeight + 'px';
    var aWamp = document.getElementsByClassName('wamp');
    var oTop = getTop(obj) + 42;
    var scrollTop = document.body.scrollTop;
    var height = bodyHeight - oTop + scrollTop;
    if (aWamp[0]) {
        aWamp[0].style.height = height + 'px';
        aWamp[0].style.maxHeight = baseHeight + 'px';
    }
    if (aWamp[1]) {
        aWamp[1].style.height = height + 'px';
        aWamp[1].style.maxHeight = baseHeight + 'px';
    }
}
